import Vue from 'vue'
import VueRouter from 'vue-router'
import { getToken } from '@/request/token'
Vue.use(VueRouter)

const routes = [
  { path: "/", component: () => import('@/views/login') },
  {
    path: "/home", redirect: "/Welcome", component: () => import('@/views/Home'),
    children: [
      { path: "/Welcome", component: () => import('@/views/Welcome') },
      { path: "/users", component: () => import('@/views/User') },
      { path: "/roles", component: () => import('@/views/roles') },
      { path: "/rights", component: () => import('@/views/rights') },
      { path: "/goods", component: () => import('@/views/goods') },
    ]
  },
  { path: "*", component: () => import('@/views/pathFound') },
]

let wiethLisy=['/404','/']
const router = new VueRouter({
  routes
})

// 页面鉴权
router.beforeEach((to, from, next) => {
  // 判断本地是否有token
  if(getToken()){
    // 有token判断是否进入的是登录页
    if(to.path=='/'){
      // 进入的是登录页 直接跳到到首页 实现免登陆功能
      next('/home')
    }else{
      // 进入的不是登录页 直接放行
      next()
    }
  }else{
    // 没有token 判断白名单中是否有
    if(wiethLisy.indexOf(to.path)>-1){
      // 在 直接进入
      next()
    }else{
      // 不在进入登录页重新登录
      next('/')
    }
  }
})
export default router
